﻿using Data;
using Data.Repositories;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace BusinessLogic
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "HoaDonService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select HoaDonService.svc or HoaDonService.svc.cs at the Solution Explorer and start debugging.
    public class HoaDonService : IHoaDonService
    {
        public string CapNhatHoaDon(HoaDon hd)
        {
            var hoadonDataTier = new HoaDonRepository();
            var result = hoadonDataTier.CapNhatHoaDon(hd);
            return result;
        }
        public List<HoaDon> DanhSachHoaDon(long UserId)
        {
            var hoadonDataTier = new HoaDonRepository();
            var Datatable = hoadonDataTier.DanhSachHoaDon(UserId);
            var lstHoaDon = new List<HoaDon>();
            
            if (Datatable != null && Datatable.Rows.Count > 0)
            {
                foreach (DataRow dr in Datatable.Rows)
                {
                    var hoadon = new HoaDon();
                    hoadon.Id = Convert.ToInt64(dr["HD_Id"]);
                    hoadon.NguoiDungId = Convert.ToInt64(dr["N_Id"]);
                    hoadon.TongTien = Convert.ToInt64(dr["HD_TongTien"]);
                    hoadon.TrangThai = Convert.ToInt32(dr["HD_TrangThai"]);
                    hoadon.ThoiGian = Convert.ToDateTime(dr["HD_ThoiGian"]);
                    hoadon.TrangThaiTen =dr["T_NoiDung"].ToString();
                    var lstChiTietHoaDon = new List<HoaDonChiTiet>();
                    
                    var datatable = hoadonDataTier.ChiTietHoaDon(hoadon.Id);
                    if (datatable != null && datatable.Rows.Count > 0)
                    {
                        int i = 1;
                        foreach (DataRow drc in datatable.Rows)
                        {
                            
                            var ChiTietHoaDon = new HoaDonChiTiet();
                            ChiTietHoaDon.STT = i;
                            ChiTietHoaDon.Id = Convert.ToInt64(drc["CHD_Id"]);
                            ChiTietHoaDon.HoaDonId = Convert.ToInt64(drc["HD_Id"]);
                            ChiTietHoaDon.SanPhamId = Convert.ToInt64(drc["S_Id"]);
                            ChiTietHoaDon.GiaTien = Convert.ToInt64(drc["S_Gia"]);
                            ChiTietHoaDon.SoLuong = Convert.ToInt64(drc["CHD_SoLuong"]);
                            ChiTietHoaDon.TrangThai = Convert.ToInt32(drc["CHD_TrangThai"]);
                            ChiTietHoaDon.ThoiGian = Convert.ToDateTime(drc["CHD_ThoiGian"]);
                            ChiTietHoaDon.SanPhamTen = drc["S_Ten"].ToString();
                            lstChiTietHoaDon.Add(ChiTietHoaDon);
                            i++;
                        }
                    }
                    hoadon.lstChiTiet = lstChiTietHoaDon;
                    lstHoaDon.Add(hoadon);
                }

            }
            return lstHoaDon;
        }
    }
}
